package com.admin.boot.modules.department.controller;

import com.admin.boot.modules.department.entity.SysDepartment;
import com.admin.boot.modules.department.service.ISysDepartmentService;

import com.admin.boot.common.utils.Result;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
* @version v3.0
* @Description: TODO
* @author: admin-boot
* @date: 2024-01-15
*/
@Api(tags = "部门表")
@RestController
@RequestMapping("/sysDepartment/sysDepartment")
public class SysDepartmentController {

    @Autowired
    private ISysDepartmentService sysDepartmentService;

    @GetMapping("/list")
    @ApiOperation(value = "部门表-分页列表查询", notes = "部门表-分页列表查询")
    public Result<?> list(SysDepartment sysDepartment,
                          @RequestParam(value = "pageNo", defaultValue = "1") Integer pageNo,
                          @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize) {
        LambdaQueryWrapper<SysDepartment> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SysDepartment::getPid,0);
        Page<SysDepartment> page = new Page<>(pageNo, pageSize);
        Page<SysDepartment> sysDepartmentPage = sysDepartmentService.page(page, queryWrapper);

        for (SysDepartment record : sysDepartmentPage.getRecords()) {
            LambdaQueryWrapper<SysDepartment> queryWrapper1 = new LambdaQueryWrapper<>();
            queryWrapper1.eq(SysDepartment::getPid,record.getId());
            List<SysDepartment> list = sysDepartmentService.list(queryWrapper1);
            record.setChildren(list);
        }
        return Result.OK(sysDepartmentPage);
    }


    @GetMapping("/listFirstLevel")
    @ApiOperation(value = "部门表-分页列表查询", notes = "部门表-分页列表查询")
    public Result<?> listFirstLevel(SysDepartment sysDepartment) {
        LambdaQueryWrapper<SysDepartment> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SysDepartment::getPid,0);
        queryWrapper.eq(SysDepartment::getStatus,1);
        List<SysDepartment> list = sysDepartmentService.list(queryWrapper);

        return Result.OK(list);
    }

    @GetMapping("/queryList")
    @ApiOperation(value = "用户表-分页列表查询", notes = "用户表-分页列表查询")
    public Result<?> queryList(SysDepartment sysDepartment) {
        LambdaQueryWrapper<SysDepartment> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SysDepartment::getStatus,1);
        List<SysDepartment> list = sysDepartmentService.list(queryWrapper);

        return Result.OK(list);
    }

    @PostMapping("/add")
    @ApiOperation(value = "部门表-添加", notes = "部门表-添加")
    public Result<?> add(@RequestBody SysDepartment sysDepartment) {
        sysDepartmentService.save(sysDepartment);
        return Result.OK("添加成功！");
    }


    @PostMapping("/edit")
    @ApiOperation(value = "部门表-编辑", notes = "部门表-编辑")
    public Result<?> edit(@RequestBody SysDepartment sysDepartment) {
        sysDepartmentService.updateById(sysDepartment);
        return Result.OK("编辑成功！");
    }

    @GetMapping("/deleteById")
    @ApiOperation(value = "部门表-删除", notes = "部门表-删除")
    public Result<?> deleteById(String id) {
        sysDepartmentService.removeById(id);
        return Result.OK("删除成功！");
    }

    @GetMapping("/deleteBatch")
    @ApiOperation(value = "部门表-批量删除", notes = "部门表-批量删除")
    public Result<?> deleteBatch(String ids) {
        for (String id : ids.split(",")) {
            sysDepartmentService.removeById(id);
        }
        return Result.OK("删除成功！");
    }

}
